package com.fileDemo;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: FENGYiMing
 * @Date: 2024/04/08/14:19
 * @Description:
 */
public class FileCopy {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            int resData = 0;
            fileInputStream = new FileInputStream("e:/bz.jpg");
            fileOutputStream = new FileOutputStream("e:/bz3.jpg");

            byte[] buf = new byte[1024];
            while ((resData = fileInputStream.read(buf)) != -1) {
                // 这样可能导致文件损失
                fileOutputStream.write(buf);
                //buf, 0, resData   这样不会
            }
            System.out.println("复制成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        }

    }
}
